<?php
/*
 * 分词类.
 * @package    class
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: a5e126d13a7c0d038488db702c6cf5f083300d44 $
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 分词空间
 * Class SCWS
 */
class SCWS {

	const CHARSET_UTF8 = 'utf8';
	const CHARSET_GBK  = 'gbk';

	private $_handle;

	/**
	 * constructor
	 * @param string $charset
	 * @param        $rule
	 * @param        $dict
	 */
	public function __construct($charset = SCWS::CHARSET_UTF8, $rule, $dict) {
		$this->_handle = scws_new();
		$this->setCharset($charset);
		$this->setDict($dict);
		$this->setRule($rule);
	}

	/**
	 * 设置字符集
	 * @param $charset
	 * @return mixed
	 */
	public function setCharset($charset) {
		return $this->_handle->set_charset($charset);
	}

	/**
	 * 设置忽略
	 * @param $is
	 * @return mixed
	 */
	public function setIgnore($is) {
		return $this->_handle->set_ignore($is);
	}

	/**
	 * 设置多提取
	 * @param $is
	 * @return mixed
	 */
	public function setMulti($is) {
		return $this->_handle->set_multi($is);
	}

	/**
	 * 设置词典
	 * @param $path     词典地址
	 * /usr/local/scws/etc/dict.xdb
	 * @return mixed
	 */
	public function setDict($path) {
		return $this->_handle->set_dict($path);
	}

	/**
	 * 设置规则
	 * @param $rule     规则地址
	 *  /path/to/rules.ini
	 * @return mixed
	 */
	public function setRule($rule) {
		return $this->_handle->set_rule($rule);
	}

	/**
	 * 设置文本
	 * @param $content
	 * @return mixed
	 */
	public function setText($content) {
		return $this->_handle->send_text($content);
	}

	/**
	 * 设置结果
	 * @return mixed
	 */
	public function getResult() {
		return $this->_handle->get_result();
	}

	/**
	 * 获取所有结果
	 * @return array
	 */
	public function getResults() {
		$results = array();
		while($result = $this->_handle->get_result()) {
			$results[] = $result;
		}
		return $results;
	}

	/**
	 * 获取top关键词
	 * @param int $num
	 * @return mixed
	 */
	public function getTops($num = 10) {
		return $this->_handle->get_tops($num);
	}

	public function close() {
		return $this->_handle->close();
	}
}